package project;

import java.util.regex.Pattern;

public enum Command {
	
	NEW		("new", "^[0-2]$", "^[a-i][1-9](h|v)?$", "new (AI Players 0-2) [initial moves]"), 
	LOAD	("load", "^\\w*$", "load (Filename)"), 
	SAVE	("save", "^\\w*$", "save (Filename)"),
	MOVE	("move", "^[a-i][1-9](h|v)?$", "move (your move)"), 
	SETAI	("setai", "^(minimax|sensible|random)$", "^-d[0-9]+$", "^-t[0-9]+$", "setai (minimax|sensible|random) [-dDepth] [-tTimeout]"),
	UNDO	("undo", "undo"),
	REDO	("redo", "redo"),
	EXIT	("exit", "exit"),
	VALID	("valid", "^([a-i][1-9](h|v)?|all)$", "^-w$", "^-p$", "valid [move] [[all] [-w] [-p]]");
	
	private final String name;
	private Pattern required;
	private Pattern optional;
	private Pattern optional2;
	private String usage;
	
	Command(String name, String usage) {
		this.name = name;
		this.usage = usage;
	}
	
	Command(String name, String required, String usage) {
		this.name = name;
		this.required = Pattern.compile(required);
		this.usage = usage;
	}
	
	Command(String name, String required, String optional, String usage) {
		this.name = name;
		this.required = Pattern.compile(required);
		this.optional = Pattern.compile(optional);
		this.usage = usage;
	}
	
	Command(String name, String required, String optional, String optional2, String usage) {
		this.name = name;
		this.required = Pattern.compile(required);
		this.optional = Pattern.compile(optional);
		this.optional2 = Pattern.compile(optional2);
		this.usage = usage;
	}
	
	static Command getCommand(String name) {
		for (Command command : Command.values()) {
			if (command.name.equals(name)) {
				return command;
			}
		}
		return null;
	}
	
	String getUsage() {
		return usage;
	}
	
	boolean isRequired(String argument) {
		return required.matcher(argument).matches();
	}
	
	boolean isOptional(String argument) {
		if (optional == null) {
			return false;
		}
		return optional.matcher(argument).matches() || (optional2 != null && optional2.matcher(argument).matches());
	}
	
	boolean noArgs() {
		return required == null;
	}
	
	public String toString() {
		return name;
	}
}
